#!/bin/bash
# 两个网口之间做nat，eth0连接外部网络，eth1为内部网络的网关
# 通过做nat转换，内部网络访问外部网络

EXTIE="eth0"
INIF="eth1"              
INNET="192.168.0.251/24" 

# NAT table
iptables -F -t nat
iptables -X -t nat
iptables -Z -t nat
iptables -t nat -P PREROUTING  ACCEPT
iptables -t nat -P POSTROUTING ACCEPT
iptables -t nat -P OUTPUT      ACCEPT

iptables -A INPUT -i $INIF -j ACCEPT
echo "1" > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -s $INNET -o $EXTIE -j MASQUERADE

/etc/init.d/iptables save

# add route
gw=""
if [ -z "gw" ]; then
	gw=`ifconfig eth0 |grep "inet addr:" |awk '{print $2}'|awk -F : '{print $2}'|awk -F . '{print $1 "." $2 "." $3}'`.1
fi
route del default
route add -net default netmask 0.0.0.0 gw $gw eth0

exit 0
